package com.orhon.smartcampus.modules.baseinfo.fetcher;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.baseinfo.dao.SchoolcalendarDayDao;
import com.orhon.smartcampus.modules.baseinfo.entity.SchoolcalendarDay;
import com.orhon.smartcampus.modules.baseinfo.entity.SchoolcalendarWeek;
import com.orhon.smartcampus.modules.baseinfo.entity.Semester;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSchoolCalendarDay;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSchoolCalendarWeek;
import com.orhon.smartcampus.modules.baseinfo.gqlModel.GQLSemester;
import com.orhon.smartcampus.modules.baseinfo.mm.SchoolCalendarDayMapper;
import com.orhon.smartcampus.modules.baseinfo.mm.SchoolCalendarWeekMapper;
import com.orhon.smartcampus.modules.baseinfo.mm.SemesterMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class SchoolCalendarDaysFetcher implements DataFetcher {


	@Autowired
	SchoolcalendarDayDao schoolcalendarDayDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		List<GQLSchoolCalendarDay> GQLSchoolCalendarDay = new ArrayList<>();

		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from SchoolcalendarDay where 1 = 1 ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));

			if(map.get("year_code")!=null) {
				hql += "and year_code "+ Util.decide(map.get("year_code"));
			}
			if(map.get("school_id")!=null) {
				hql += "and school_id  " + Util.decide(map.get("school_id"));
			}
			if(map.get("semester_id")!=null) {
				hql += "and semester_id " + Util.decide(map.get("semester_id"));
			}
			if(map.get("week_id")!=null) {
				hql += "and week_id " + Util.decide(map.get("week_id"));
			}

		}

		List<SchoolcalendarDay> list = schoolcalendarDayDao.find(hql,null,page.getPage(),page.getLimit());

		for (SchoolcalendarDay item : list) {
			GQLSchoolCalendarWeek gqlSchoolCalendarWeek= null;
			if (env.getSelectionSet().contains("data/week")){
				SchoolcalendarWeek week = item.getSchoolcalendarWeek();
				gqlSchoolCalendarWeek = SchoolCalendarWeekMapper.MAPPER.toSchoolcalendarWeek(week);
			}
			
			GQLSemester gqlSemester = null;
			if (env.getSelectionSet().contains("data/semester")){
				Semester semester = item.getSemester();
				gqlSemester = SemesterMapper.MAPPER.toSemester(semester);
			}

		    GQLSchoolCalendarDay schoolCalendarDay = SchoolCalendarDayMapper.MAPPER.toSchoolCalendarDay(item);
		    schoolCalendarDay.setSemester(gqlSemester);
		    schoolCalendarDay.setWeek(gqlSchoolCalendarWeek);
		    GQLSchoolCalendarDay.add(schoolCalendarDay);
		}
		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = schoolcalendarDayDao.getTotalCount(hql);
		}

		return Result.ok().put("data" , GQLSchoolCalendarDay).put("total" , total);
	}


}
